var intID;
$(function() {
    $('.wymeditor').wymeditor({
   
        
      //classes panel
      classesItems: [
        {'name': 'date', 'title': 'PARA: Date', 'expr': 'p'},
        {'name': 'hidden-note', 'title': 'PARA: Hidden note',
         'expr': 'p[@class!="important"]'},
        {'name': 'important', 'title': 'PARA: Important',
         'expr': 'p[@class!="hidden-note"]'},
        {'name': 'border', 'title': 'IMG: Border', 'expr': 'img'},
        {'name': 'special', 'title': 'LIST: Special', 'expr': 'ul, ol'}
      ],
      
      //we customize the XHTML structure of WYMeditor by overwriting 
      //the value of boxHtml. In this example, "CONTAINERS" and 
      //"CLASSES" have been moved from "wym_area_right" to "wym_area_top":
      boxHtml:   "<div class='wym_box'>"
              + "<div class='wym_area_top'>"
              + WYMeditor.TOOLS
              + WYMeditor.CONTAINERS
              + WYMeditor.CLASSES
              + "</div>"
              + "<div class='wym_area_left'></div>"
              + "<div class='wym_area_right'>"
              + "</div>"
              + "<div class='wym_area_main'>"
              + WYMeditor.HTML
              + WYMeditor.IFRAME
              + WYMeditor.STATUS
              + "</div>"
              + "<div class='wym_area_bottom'>"
              + "</div>"
              + "</div>",
      stylesheet: '_lib/wymeditor/styles.css',
      //postInit is a function called when WYMeditor instance is ready
      //wym is the WYMeditor instance
      postInit: function(wym) {
      //we make all sections in area_top render as dropdown menus:
        $(wym._box)
            //first we have to select them:
            .find(".wym_area_top .wym_section")
            //then we remove the existing class which make some of them render as a panels:
            .removeClass("wym_panel")
            //then we add the class which will make them render as a dropdown menu:
            .addClass("wym_dropdown")
            //finally we add some css to make the dropdown menus look better:
            .css("width", "80px")
            .css("float", "left")
            .css("margin-right", "5px")
            .find("ul")
            .css("width", "140px");

        //add a ">" character to the title of the new dropdown menus (visual cue)
        $(wym._box).find(".wym_tools, .wym_classes ")
            .find(WYMeditor.H2)
            .append("<span>&nbsp;&gt;</span>");
        }
       
    });

  var wym = jQuery.wymeditors(0);
  
	$("#folioge").jstree({ 
 
     "xml_data" : { 
				"ajax" : {
					"url" : "./folioge.php",
					"data" : function (n) { 
						return { 
							"hack" : "tree_xml"
						}; 
					}
				},
  			"xsl" : "flat"
			},
		

			"plugins" : [ "themes", "xml_data", "crrm", "ui", "dnd" ] 
	       
		})
	    

    	.bind("select_node.jstree", function (e, data) {
      $("#view_note").html("<img src=\"themes/default/throbber.gif\" />");
			$.post(  "./folioge.php",	{ 
					"operation" : "note_get", 
					"id" : data.rslt.obj.attr("id"), 
					"title" : data.inst.get_text(data.rslt.obj)
				}, 
				function (r) {
			
					$("#view_note").html(r);
					$("#view").show();
          $("#editor").hide();
				}
			); 
		})
		.bind("reselect.jstree", function (event, data) { 
       data.inst.select_node('ul > li:first'); 
    }) 
			.bind("create.jstree", function (e, data) {
			var parent = data.rslt.parent;
			if(parent == -1) {
			 parent = "";
      } else {
        parent =   data.inst._get_parent(data.rslt.obj).attr("id");
      }
			$.post("./folioge.php",	{ 
					"operation" : "create_node", 
					"id" : data.rslt.obj.attr("id"), 
					"parent" : parent,
					"title" : data.inst.get_text(data.rslt.obj)
				}, 
				function (r) {
						$(data.rslt.obj).attr("id",  r);
						data.inst.deselect_all();
						 data.inst.select_node(data.rslt.obj);
				}
			);
		})
		.bind("rename.jstree", function (e, data) {
			$.post(
				"./folioge.php", 
				{ 
					"operation" : "rename_node", 
					"id" : data.rslt.obj.attr("id"),
					"title" : data.rslt.new_name,
				}, 
				function (r) {
					var x = r;
				}
			);
		})
		.bind("remove.jstree", function (e, data) {
			$.post(
				"./folioge.php", 
				{ 
					"operation" : "remove_node", 
					"id" : data.rslt.obj.attr("id")
				}, 
				function (r) {
					alert(r);
					data.inst.select_node(data.inst._get_node("#"+intID));;
				}
			);
		})
		.bind("move_node.jstree", function (e, data) {
		
			var parent = data.rslt.np.attr("id");
			if(parent == "folioge") {
			 parent = "";
      } 
			$.post(
				"./folioge.php", 
				{ 
					"operation" : "move_node", 
					"id" : data.rslt.o.attr("id"), 
					"parent" : parent
				}, 
				function (r) {
					var x = r;
				}
			);
		});


    $('#test').click(function () {
   //$('#folioge').jstree('select_node', 'ul > li:first'); 
      $.jstree._focused().select_node($.jstree._focused()._get_node("#6"));
     });
  $('#create_root').click(function () {
      $("#folioge").jstree("create", 0, "last","Enter a new name." );
     });
  $('#create_node').click(function () {
    if (!$.jstree._focused()._get_node()) {
      alert("You must select a node first.");
    } else {
      $("#folioge").jstree("create",null,"last","Enter a new name.");
    }
  });
  $('#rename_node').click(function () {
    if (!$.jstree._focused()._get_node()) {
      alert("You must select a node first.");
    } else {
      $("#folioge").jstree("rename");
    }
  });
  $('#open_all').click(function () {
      $('li', '#folioge').removeClass("jstree-closed").addClass("jstree-open"); 
       //$.jstree._focused().open_all(-1);
  });
  $('#close_all').click(function () {
      $('li', '#folioge').removeClass("jstree-open").addClass("jstree-closed"); 
       //$.jstree._focused().close_all(-1);
  });
  $('#remove_branch').click(function () {
    if(confirm('Delete this branch?')) { 
      if (!$.jstree._focused()._get_node()) {
        alert("You must select a node first.");
      } else {
        var parent = $.jstree._focused()._get_parent();
  			if(parent == -1) {
  			 parent = "";
        } else {
          parent = $.jstree._focused()._get_parent($.jstree._focused()._get_node()).attr("id");
        }
        intID = parent;
        $("#folioge").jstree("remove");
      }
    }
  });
  
  $('#edit_note').click(function() {
    if (!$.jstree._focused()._get_node()) {
      alert("You must select a node first.");
    } else {
      $("#view").hide();
      wym.html($("#view_note").html());
      $("#editor").show();
    }
  });
  $('#cancel_note').click(function() {
    $("#view").show();
    $("#editor").hide();
  });
  $('#submit_note').click(function() {
    if (!$.jstree._focused()._get_node()) {
      alert("You must select a node first.");
    } else {
      $("#editor").hide();
      $("#view_note").html(wym.html());
      $("#view").show();
       
      $.post("folioge.php", {"operation" : "note_submit", "id" : $.jstree._focused()._get_node().attr("id"), "note" : wym.xhtml() }, function(data){
            if (data.length>=0){
              var r = data;
            } 
          });
      }
  });
  
  $('#search_tree').click(function() {
    $("#search_wrap").show();
    $("#tree_wrap").hide();
    $("#search_text").val("");
    $("#replace_text").val("");
    $("#search_returned").html("")
  });
      
  $('#cancel_search').click(function() {
    $("#search_wrap").hide();
    $("#tree_wrap").show();
  });
  
  $('#toggle_replace').click(function() {
    $("#replace_ask").hide();
    $("#replace_show").show();
  });
  
  $('#clear_replace').click(function() {
    $("#replace_ask").show();
    $("#replace_text").val("");
    $("#replace_show").hide();
  });
  $('#submit_search').click(function() {
      $("#search_returned").html("<li><img src=\"themes/default/throbber.gif\" /></li>");

      $.post("folioge.php", {
            "operation" : "search_replace", 
            "search":$("#search_text").val(), 
            "replace":$("#replace_text").val()
            }, 
            function(data){
            if (data.length>=0){
              $("#search_returned").html(data);
            } 
          });
  });
});  
  
